/*
 * DocHandler.java
 *
 * Created on 3 September 2006, 7:51
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fasea.index.filehandler;

import java.io.FileNotFoundException;
import org.apache.lucene.document.Field;

import org.textmining.text.extraction.WordExtractor;

import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import org.apache.lucene.document.Document;

/**
 *
 * @author gvasil
 */
class DOCHandler extends FileHandler {
    
    public Document getTypedDocument(File file) throws FileHandlerException {
        InputStream is = null;
        try {
            is = new FileInputStream(file);
        }
        catch (FileNotFoundException e) {
            throw new FileHandlerException(
                    "File not found: "
                    + file.getAbsolutePath(), e);
        }
        
        org.apache.lucene.document.Document doc = createDefaultLuceneDocument(file);
        
        String text = null;
        try {
            text = getText(is);
            /* Eat consecutive whitespace characters */
       //     text = text.replaceAll("\\s+", " ");
        }
        catch (Exception e) {
            throw new FileHandlerException(
                    "Cannot extract text from the word document", e);
        }

        if ((text != null) && (text.trim().length() > 0)) {
            doc.add(new Field("content", text, Field.Store.NO,
                    Field.Index.ANALYZED, Field.TermVector.WITH_OFFSETS));
        }
        return doc;
    }
    
    
    private String getText(InputStream is) throws Exception {
        return new WordExtractor().extractText(is);
    }
}

